var express = require('express')
var path = require('path')
var cookieParser = require('cookie-parser')
var logger = require('morgan')
const bodyParser = require('body-parser')
var expressJWT = require('express-jwt')

// 导入cors包
const cors = require('cors')

const userRouter = require('./routes/gg_user')
const noticeRouter = require('./routes/gg_notice')
const reportRouter = require('./routes/gg_report')
const auditEventRouter = require('./routes/gg_auditEvent')
const eventRouter = require('./routes/gg_event')
/* 导入登录在线相关的路由 */
const onlineRouter = require('./routes/online')
// 导入登录和验证的路由
var loginRouter = require('./routes/login_verify')
var validateRouter = require('./routes/validate')
var app = express()
// 统一的加密密钥
const secretKey = 'guanggu'
// jwt中间件
app.use(
  expressJWT({
    algorithms: ['HS256'],
    secret: secretKey, //加密密钥，可换
  }).unless({
    //添加不需要token验证的路由 ==> 包括前台路由和登录和验证的路由
    path: [
      /^\/login/,
      /^\/validate/,
      /^\/user/,
      // 正则表达式^表示以什么开头  这个正则表达式的含义为以/login开头
    ],
  })
)
// 增大可传递的数据大小值
app.use(bodyParser.json({ limit: '50mb' }))
app.use(bodyParser.urlencoded({ limit: '50mb', extended: true }))

//静态伺服
app.use(express.static('page'))

// 注册全局中间件
app.use(cors())
app.use(logger('dev'))
app.use(express.json())
app.use(express.urlencoded({ extended: false }))
app.use(cookieParser())
app.use(express.static(path.join(__dirname, 'public')))

//
app.use('/user', userRouter)
app.use('/notice', noticeRouter)
app.use('/report', reportRouter)
app.use('/auditEvent', auditEventRouter)
app.use('/event', eventRouter)
// 挂载登录路由
app.use('/login', loginRouter)
app.use('/validate', validateRouter)
app.use('/online', onlineRouter)

module.exports = app
